package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(readOnly = true)
    public List<User> findAll() {
        //int i =1/0;
        List<User> list = userDao.findAll();
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list = userDao.findAll();
        PageInfo<User> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional(readOnly = true)
    public User findById(Integer id) {
        User user = userDao.findById(id);
        return user;
    }

    @Override
    public int save(User user) {
        int count = userDao.save(user);
        return count;
    }

    @Override
    public int update(User user) {
        int count = userDao.update(user);
        return count;
    }

    @Override
    public int delete(Integer id) {
        int count = userDao.delete(id);
        return count;
    }

    @Override
    @Transactional(readOnly = true)
    public User login(String username, String password) {
        User loginUser = userDao.findUserByUsernameAndPassword(username, password);
        return loginUser;
    }
}
